package com.boe.hbd.server.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.boe.hbd.server.processor.Processor;

public class MainServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5402874960380395280L;

	public void init() {
		InitCache.initViewCache();
		InitCache.initPicSeqCache();

	}

	@Override
	protected void doGet(final HttpServletRequest req,
			final HttpServletResponse resp) throws ServletException,
			IOException {
		String result = "ERROR";
		try {
			String path = req.getServletPath().toLowerCase();
			String[] pa = path.split("/");
			Processor processor = RestfulMaping.getProcessor(pa);
			result = processor.process(pa, req);
		} catch (Throwable e) {
			// FIXME do something, write log
		}
		final PrintWriter out = resp.getWriter();
		out.println(result);
		out.flush();
		out.close();
	}

	protected void doPost(final HttpServletRequest req,
			final HttpServletResponse resp) throws ServletException,
			IOException {
		doGet(req, resp);
	}

}
